package com.ta.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ta.gulimall.common.constant.ProductConstant;
import com.ta.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.ta.gulimall.product.service.AttrAttrgroupRelationService;
import com.ta.gulimall.product.service.AttrService;
import com.ta.gulimall.product.service.CategoryService;
import com.ta.gulimall.product.view.entity.AttrAttrgroupRelationEntity;
import com.ta.gulimall.product.view.entity.AttrEntity;
import com.ta.gulimall.product.view.vo.AttrGroupVo;
import com.ta.gulimall.product.view.vo.AttrGroupVoResponse;
import com.ta.gulimall.product.view.vo.AttrgroupWithAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;

import com.ta.gulimall.product.dao.AttrGroupDao;
import com.ta.gulimall.product.view.entity.AttrGroupEntity;
import com.ta.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(!ProductConstant.LEVEL1.equals(catelogId), AttrGroupEntity::getCatelogId, catelogId)
                .and(StringUtils.isNotBlank(key), eq -> eq.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key));
        IPage<AttrGroupEntity> page = page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public AttrGroupVoResponse getAttrGroup(Long attrGroupId) {
        AttrGroupEntity attrGroup = this.getById(attrGroupId);
        AttrGroupVoResponse attrGroupVoResponse = BeanUtil.copyProperties(attrGroup, AttrGroupVoResponse.class);

        List<Long> path = categoryService.getClassifyPath(attrGroupVoResponse.getCatelogId());
        attrGroupVoResponse.setCatelogPath(path);
        return attrGroupVoResponse;
    }

    @Override
    public List<AttrEntity> getAttrs(Long attrgroupId) {
        return attrService.getAttrs(attrgroupId);
    }

    @Override
    public void deleteAttrGroupRelevances(List<AttrGroupVo> attrGroupVos) {
        attrAttrgroupRelationDao.deleteAttrAttrgroupRelations(attrGroupVos);
    }

    @Override
    public PageUtils pageAttrNoRelation(Map<String, Object> params, Long attrGroupId) {
        Long catelogId = this.getById(attrGroupId).getCatelogId();
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId);
        List<Long> attrGroupIds = this.list(wrapper).stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());

        List<Long> attrIds = attrAttrgroupRelationService.getAttrAttrgroupRelations(attrGroupIds).stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        IPage<AttrEntity> page = attrService.pageAttrs(params, catelogId);
        List<AttrEntity> attrs = page.getRecords().stream().filter(f -> !attrIds.remove(f.getAttrId())).collect(Collectors.toList());
        return new PageUtils(page).setList(attrs).setTotalCount(attrs.size());
    }

    @Override
    public void insertRelationAttrs(List<AttrGroupVo> attrGroupVos) {
        List<AttrAttrgroupRelationEntity> attrgroupRelations = attrGroupVos.stream().map(m -> BeanUtil.copyProperties(m, AttrAttrgroupRelationEntity.class)).collect(Collectors.toList());
        attrAttrgroupRelationService.saveOrUpdateBatch(attrgroupRelations);
    }

    @Override
    public List<AttrgroupWithAttrVo> attrgroupWithAttrs(Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId);
        List<AttrGroupEntity> groupEntities = Optional.ofNullable(this.list(wrapper)).orElse(new ArrayList<>());

        return groupEntities.stream().map(this::getAttrgroupWithAttrVos).collect(Collectors.toList());
    }

    private AttrgroupWithAttrVo getAttrgroupWithAttrVos(AttrGroupEntity attrGroupEntity) {
        AttrgroupWithAttrVo attrgroupWithAttrVo = BeanUtil.copyProperties(attrGroupEntity, AttrgroupWithAttrVo.class);
        attrgroupWithAttrVo.setAttrs(attrService.getAttrs(attrGroupEntity.getAttrGroupId()));

        return attrgroupWithAttrVo;
    }

}